/*
 * Copyright 2019 Patrik Karlström.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package se.trixon.almond.util.swing;

import java.awt.Color;
import java.util.Arrays;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import se.trixon.almond.util.Dict;

/**
 *
 * @author Patrik Karlström
 */
public class ColorPanel extends javax.swing.JPanel {

    private Color mColor;
    private final JColorChooser mColorChooser = new JColorChooser();
    private JComponent mDialogParent = null;
    private ColorChangeListener mColorChangeListener;

    /**
     * Creates new form ColorPanel
     */
    public ColorPanel() {
        initComponents();
        setColor(Color.BLACK);
    }

    public Color getColor() {
        return mColor;
    }

    public ColorChangeListener getColorChangeListener() {
        return mColorChangeListener;
    }

    public void setColor(Color color) {
        mColor = color;
        setBackground(color);
    }

    public void setColorChangeListener(ColorChangeListener colorChangeListener) {
        mColorChangeListener = colorChangeListener;
    }

    public void setDialogParent(JComponent dialogParent) {
        mDialogParent = dialogParent;
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(102, 255, 51));
        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 396, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 296, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        if (!isEnabled()) {
            return;
        }

        Color oldColor = new Color(mColor.getRGB());
        mColorChooser.setColor(mColor);

        Object[] options = new Object[]{Dict.CANCEL, Dict.OK};

        int retval = JOptionPane.showOptionDialog(mDialogParent,
                mColorChooser,
                Dict.Dialog.TITLE_SELECT_COLOR.toString(),
                JOptionPane.DEFAULT_OPTION,
                JOptionPane.PLAIN_MESSAGE,
                null,
                options,
                Dict.OK);

        if (retval == Arrays.asList(options).indexOf(Dict.OK)) {
            setColor(mColorChooser.getColor());
            if (mColorChangeListener != null && !oldColor.equals(mColor)) {
                mColorChangeListener.onColorChanged(oldColor, mColor);
            }
        }
    }//GEN-LAST:event_formMousePressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    public interface ColorChangeListener {

        void onColorChanged(Color oldColor, Color newColor);
    }
}
